/**
 * @file task_test.c
 * @brief 
 * @author Tome (baojian.liu-a1105@aqara.com)
 * @date 2021-01-05
 */
 

#include "OSAL.h" 
#include "osal_log.h"
#include "stdio.h"

#include "task_uart.h"
#include "hal_led.h"
#include "hal_uart.h"


 
/****************************************************************************************************************************** 
                                                            const
*******************************************************************************************************************************/












/****************************************************************************************************************************** 
                                                            typedef
*******************************************************************************************************************************/













/****************************************************************************************************************************** 
                                                            MICRO
*******************************************************************************************************************************/




/****************************************************************************************************************************** 
                                                            declearation
*******************************************************************************************************************************/


static void task_uart_ProcessOSALMsg(osal_event_hdr_t *pMsg );
static void task_test_HandleUarts ( uart_event_e evt );
/****************************************************************************************************************************** 
                                                            defination
*******************************************************************************************************************************/



static uint8 Uart_TaskID;


/**************************************************************************************************
 * @fn      Hal_Init
 *
 * @brief   Hal Initialization function.
 *
 * @param   task_id - Hal TaskId
 *
 * @return  None
 **************************************************************************************************/
void Task_Uart_Init( uint8 task_id )
{
   
  /* Register task ID */
  Uart_TaskID = task_id;
    
  osal_pwrmgr_task_state( task_id, PWRMGR_HOLD);
  RegisterForUart( Uart_TaskID );  
    
  LOG("task uart %d start", task_id);
  
  
}





/**************************************************************************************************
 * @fn      Test_ProcessEvent
 *
 * @brief   Test Process Event
 *
 * @param   task_id - Hal TaskId
 *          events - events
 *
 * @return  None
 **************************************************************************************************/
uint16 Task_Uart_ProcessEvent( uint8 task_id, uint16 events )
{
  uint8 *msgPtr;
    
  (void)task_id;  // Intentionally unreferenced parameter

  if ( events & SYS_EVENT_MSG )
  {
        msgPtr = osal_msg_receive(task_id);

        while (msgPtr)
        {
          /* Do something here - for now, just deallocate the msg and move on */
            
          //LOG("T %02d, %s", task_id,msgPtr);
            
           task_uart_ProcessOSALMsg((osal_event_hdr_t *)msgPtr );     

          /* De-allocate */
          osal_msg_deallocate( msgPtr );
          /* Next */
          msgPtr = osal_msg_receive( task_id );
        }
      return (events ^ SYS_EVENT_MSG );
  }
  
  

   return 0;
}


 
/******************************************************************************
* @fn        task_uart_ProcessOSALMsg
*
* @brief     osal system events handler
*
* @param     
*
* @return    none
*/
static void task_uart_ProcessOSALMsg(osal_event_hdr_t *pMsg )
{
switch ( pMsg->event )
  {
  case UART_EVENT:
    task_test_HandleUarts ( ((uartEvent_t*)pMsg)->evt );
    
    break;

    
  default:
    break;
  }  
}



/*********************************************************************
 * @fn      funcname
 *
 * @brief   
 *
 * @param   
 *
 * @param   
 *
 * @param   
 *
 * @return  
 */

void task_test_HandleUarts ( uart_event_e evt )
{
	uint8 tempbuf[256];
	uint16 size;
    switch(evt)
    {
        case UART_RECV_DATA:
            
			size = HalUartGetRecvUsedSize();
			LOG_RAW("\n UART_RECV_DATA , LEN:%d\n",size);
		    size = HalUartRead(tempbuf,sizeof(tempbuf));
			tempbuf[size] = '\0';
			LOG_RAW(" DATA:");
			LOG_RAW((const char *)tempbuf);
            break;
        
		case UART_SEND_COMPLETE:
			// Don't print a serial port here or you'll get caught in the event loop
			break;
		
        
        default:
            break;
    
    }   
}

/*********************************************************************
 * @fn      funcname
 *
 * @brief   
 *
 * @param   
 *
 * @param   
 *
 * @param   
 *
 * @return  
 */


/*********************************************************************
 * @fn      funcname
 *
 * @brief   
 *
 * @param   
 *
 * @param   
 *
 * @param   
 *
 * @return  
 */


/*********************************************************************
 * @fn      funcname
 *
 * @brief   
 *
 * @param   
 *
 * @param   
 *
 * @param   
 *
 * @return  
 */ 





